﻿#include "guild_application_logic.h"

#include "guild/guild_loader.hpp"
#include "guild/module/guild_module.h"
#include "guild/module/guild_module_loader.hpp"
#include "error/make_error_code.h"

namespace guild_service {
bool GuildApplicationLogic::GetGuildModuleApplication(const guild_id_type guild_id,
                                                GuildModuleApplication &application) {
  return detail::GetGuildModule<GuildModuleApplication>(
      kGuildServiceQueryName, guild_id, kGuildModuleApplicationName, application);
}

bool GuildApplicationLogic::GetGuildModuleApplication(const role_uuid_type role_uuid,
                                                GuildModuleApplication &application,
                                                error_code &ec) {
  if (role_uuid == kInvalidRoleUuid) {
    ec = ErrorCode::ERR_INVALID_GUILD_ID;
    return false;
  }

  guild_id_type guild_id = kInvalidGuildId;
  if (!detail::LoadGuildIdByRole(kGuildServiceQueryName, role_uuid, guild_id)) {
    ec = ErrorCode::ERR_GUILD_NOT_EXIST;
    return false;
  }

  if (!GuildApplicationLogic::GetGuildModuleApplication(guild_id, application)) {
    ec = ErrorCode::ERR_GUILD_APPLICATION_LOAD_FAILED;
    return false;
  }

  return true;
}

}  // namespace guild_service